﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.RFID
{
    /// <summary>
    /// 处理RFID事物的类（非静态，需实例化）
    /// </summary>
    public class Rfid_Dal
    {
        public SGArchiveSystem7xEntities conn;
        public SGArchiveSystem7xEntities conn2;
        public Rfid_Dal()
        {
            conn = new SGArchiveSystem7xEntities();
            conn2 = new SGArchiveSystem7xEntities();
        }

        /// <summary>
        /// 每读到一个标签后，调用此方法
        /// </summary>
        /// <param name="tag">标签号</param>
        /// <param name="type">是出库房，还是进入库房</param>
        /// <returns>1. 授权的标签， 0. 未授权的标签， -1. 失败</returns>
        public int RfidTag(string tag, _ReaderType type)
        {
            try
            {
                int status = -1;
                if(type == _ReaderType.Out)
                    status = (int)SGArchiveSystem7xDB._BrrStatus._iStatus_申请;
                else
                    status = (int)SGArchiveSystem7xDB._BrrStatus._iStatus_借出;
                ///如果是出库
                ///首先查询读取到的标签是否允许出库
                var query = conn.T_BInfo.Select(c => c).Where(c => c.BI_V02 == tag && c.BI_STATUS == status);
                int count = 0;
                foreach (var n in query)
                {
                    count++;
                    #region 授权的
                    if (type == _ReaderType.Out)
                    {
                        n.BI_STATUS = (int)SGArchiveSystem7xDB._BrrStatus._iStatus_借出;
                        n.BI_D01 = DateTime.Now;
                        //n.BI_V05 = "出库";
                    }
                    else
                    {
                        n.BI_STATUS = (int)SGArchiveSystem7xDB._BrrStatus._iStatus_结束;
                        n.BI_D02 = DateTime.Now;
                        //n.BI_V05 = "入库";
                    }

                    int archiveId = n.BI_ARCHIVEID;
                    var nn = conn.T_ArchiveAj.Select(c => c).Where(c => c.ID == archiveId).ToList()[0];

                    //foreach (var nn in query2)
                    {
                        T_ArchiveJK jk = new T_ArchiveJK();
                        jk.JK_AID = nn.ID;
                        jk.JK_ArchiveDH = nn.DH;
                        jk.JK_ARID = nn.ARID;
                        jk.JK_ArchiveType = " ";///nn.AR_NAME;
                        jk.JK_Date = DateTime.Now;
                        jk.JK_ND = nn.ND;
                        jk.JK_Tag = tag;
                        jk.JK_BINFOID = n.BI_ID;
                        //if (jk.JK_ArchiveType == "诉讼档案")
                        //    jk.JK_TM = "案由:" + nn.GJZ + " 被告人:" + nn.TM;
                        //else
                        jk.JK_TM = nn.TM;
                        if (type == _ReaderType.Out)
                        {
                            jk.JK_Type = "出库";
                            nn.KFZT = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_借出;
                        }
                        else
                        {
                            jk.JK_Type = "入库";
                            nn.KFZT = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_在库;
                        }

                        jk.JK_Status = 1;
                        conn2.T_ArchiveJK.Add(jk);
                        conn2.SaveChanges();

                        nn.AJZT = (int)SGArchiveSystem7xDB._ArchiveStatus._iStatus_授权;
                        nn.I01 = jk.JK_ID;
                    }
                    #endregion
                }
                if (count > 0)
                {
                    conn.SaveChanges();
                    return 1;
                }
                else
                {
                    #region 未授权的
                    ////未授权，从档案表中找到对应的档案，然后进行处理
                    var query2 = conn.T_ArchiveAj.Select(c => c).Where(c => c.DZWJ == tag);
                    foreach(var nn in query2)
                    {
                        nn.KFZT = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_借出;
                        nn.AJZT = (int)SGArchiveSystem7xDB._ArchiveStatus._iStatus_非授权;

                        T_ArchiveJK jk = new T_ArchiveJK();
                        jk.JK_AID = nn.ID;
                        jk.JK_ArchiveDH = nn.DH;
                        jk.JK_ARID = nn.ARID;
                        jk.JK_ArchiveType = " "; ///nn.AR_NAME;
                        jk.JK_Date = DateTime.Now;
                        jk.JK_ND = nn.ND;
                        jk.JK_Tag = tag;
                        jk.JK_TM = nn.TM;
                        if (type == _ReaderType.Out)
                        {
                            jk.JK_Type = "出库";
                            nn.KFZT = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_借出;
                        }
                        else
                        {
                            jk.JK_Type = "入库";
                            nn.KFZT = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_在库;
                        }

                        jk.JK_Status = 2;
                        conn2.T_ArchiveJK.Add(jk);
                        conn2.SaveChanges();

                        nn.AJZT = (int)SGArchiveSystem7xDB._ArchiveStatus._iStatus_非授权;
                        nn.I01 = jk.JK_ID;
                    }

                    conn.SaveChanges();
                    return 0;
                    #endregion
                }
            }
            catch(Exception ex) { return -1; }
        }

        /// <summary>
        /// 每读到一个标准后，调用此方法
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public  int RfidTag_ZK(string tag, _ReaderType type)
        {
            try
            {
                int status = 0;
                ////全部设置为设备读取
                status = (int)SGArchiveSystem7xDB._BrrStatus._iStatus_申请;
                int kfzt = 0;
                if (type == _ReaderType.Out)
                    kfzt = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_借出;
                else
                    kfzt = (int)SGArchiveSystem7xDB._ArchiveKFStatus._iStatus_在库;

                var nn = conn.T_ArchiveAj.Select(c => c).Where(c => c.DZWJ == tag);
                foreach(var n in nn)
                {
                    ///
                    n.KFZT = kfzt;
                    n.AJZT = status;

                    string stype = "";
                    ///出库时间
                    if (type == _ReaderType.Out)
                    {
                        n.D01 = DateTime.Now;
                        stype = "出库";
                    }
                    else
                    {
                        stype = "入库";
                        n.D02 = DateTime.Now;
                    }

                    SGArchiveSystem7xDB.T_ArchiveJK jk = new SGArchiveSystem7xDB.T_ArchiveJK();
                    jk.JK_AID = n.ID;
                    jk.JK_ARID = n.ARID;
                    jk.JK_ArchiveDH = n.DH;
                    jk.JK_ArchiveType = " ";
                    jk.JK_BINFOID = 0;
                    jk.JK_Status = 1;
                    jk.JK_Tag = n.DZWJ;
                    jk.JK_TM = n.TM;
                    jk.JK_FTM = n.FTM;
                    jk.JK_Type = stype;
                    jk.JK_ND = n.ND;
                    jk.JK_CLR = "RFID设备";
                    jk.JK_Date = DateTime.Now; ;
                    conn.T_ArchiveJK.Add(jk);
                }
                conn.SaveChanges();
                return 1;
            }
            catch { return -1; }
        }
    }

    public enum _ReaderType
    {
        /// <summary>
        /// 进入库房
        /// </summary>
        In = 0,
        /// <summary>
        /// 离开库房
        /// </summary>
        Out = 1
    }
}
